package top.lshaci.learning.hutool;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;

import java.io.File;
import java.time.LocalDate;
import java.util.List;

/**
 * RandomUtilTest
 *
 * @author lshaci
 * @since 1.0.0
 */
public class UseRandomUtil {

    public static void main(String[] args) {
        List<HomeworkModel> data = CollUtil.newLinkedList();
        String[] symbol = {"+", "-"};
        LocalDate start = LocalDate.of(2023, 6, 27);
        LocalDate end = LocalDate.of(2023, 8, 31);
        LocalDate date = start;
        while (true) {
            HomeworkModel model = new HomeworkModel();
            model.setDate(date);
            for (int i = 0; i < 30; i++) {
                String fieldName = "js" + StrUtil.fillBefore((i + 1) + "", '0', 2);
                while (true) {
                    String s = symbol[RandomUtil.randomInt(0, 2)];
                    int m = RandomUtil.randomInt(11, 99);
                    int n = RandomUtil.randomInt(11, 99);
                    if (m % 10 == 0 || n % 10 == 0) {
                        continue;
                    }
                    int result = StrUtil.equals(s, "+") ? m + n : m - n;
                    if (result > 0 && result <= 100) {
                        ReflectUtil.setFieldValue(model, fieldName, StrUtil.format("{} {} {} = ", m, s, n));
                        break;
                    }
                }
            }
            data.add(model);
            date = date.plusDays(1);
            if (date.compareTo(end) > 0) {
                break;
            }
        }
        System.err.println("算式生成完成，即将渲染word文件");
        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
        Template template = engine.getTemplate("homework.ftl");
        template.render(Dict.create().set("dataList", data), new File("G:\\System\\Desktop\\数学作业2.doc"));
    }
}
